package hot100;

import java.util.ArrayList;
import java.util.List;

public class Lc102 {
   public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode() {}
       TreeNode(int val) { this.val = val; }
       TreeNode(int val, TreeNode left, TreeNode right) {
           this.val = val;
           this.left = left;
           this.right = right;
       }
   }

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        List<TreeNode> cur = new ArrayList<>();
        List<TreeNode> temp = new ArrayList<>();
        cur.add(root);
        while (!cur.isEmpty()) {
            List<Integer> line = new ArrayList<>(cur.size());
            for (TreeNode node : cur) {
                line.add(node.val);
                if (node.left != null) {
                    temp.add(node.left);
                }
                if (node.right != null) {
                    temp.add(node.right);
                }
            }
            ret.add(line);

            cur.clear();
            cur.addAll(temp);
            temp.clear();
        }
        return ret;
    }
}
